<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-body">
                    <div class="widget-head am-cf">
                        <div class="widget-title am-fl">收款码设置</div>
                    </div>

                    <div id="apps"  class="poster-pannel am-cf am-padding-bottom-xl">
                        <div class="pannel__left am-fl">
                            <div id="j-previews" ref="preview" class="poster-preview">
                                <img id="preview-backdrops" class="backdrop" src="<?= $data ?>" alt="">
                            </div>
                        </div>

                        <div class="pannel__right am-fl">

                            <div class="am-u-sm-9 am-u-sm-push-3 am-margin-top-lg">
<!--                                <button class="j-submit am-btn am-btn-secondary" >生成二维码</button>-->
<!--                                <span style="padding-left: 20px"></span>-->
                                <button type="submit"  class="j-down am-btn am-btn-secondary" >
                                    <a href="javascript:void(0);"  id="file-down" data-file="<?=$qdata['file']?>"   data-name="<?=$qdata['name']?>" >下载二维码</a>
                                </button>

                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 文件库弹窗 -->
{{include file="layouts/_template/file_library" /}}

<script src="assets/common/js/vue.min.js"></script>
<script>

    $(function () {
        //
        //var appVue = new Vue({
        //    el: '#app',
        //    data: <?//= $data ?>//,
        //    created: function () {
        //        /**
        //         * 注册拖拽事件
        //         */
        //        this.$nextTick(function () {
        //            this.dragEvent('qrcode');
        //            this.dragEvent('storeName');
        //        });
        //    },
        //    methods: {
        //        /**
        //         * 注册拖拽事件
        //         * @param ele
        //         */
        //        dragEvent: function (ele) {
        //            var _this = this
        //                , $preview = this.$refs.preview
        //                , $ele = this.$refs[ele]
        //                , l = 0
        //                , t = 0
        //                , r = $preview.offsetWidth - $ele.offsetWidth
        //                , b = $preview.offsetHeight - $ele.offsetHeight;
        //            $ele.onmousedown = function (ev) {
        //                var sentX = ev.clientX - $ele.offsetLeft;
        //                var sentY = ev.clientY - $ele.offsetTop;
        //                document.onmousemove = function (ev) {
        //                    var slideLeft = ev.clientX - sentX;
        //                    var slideTop = ev.clientY - sentY;
        //                    slideLeft <= l && (slideLeft = l);
        //                    slideLeft >= r && (slideLeft = r);
        //                    slideTop <= t && (slideTop = t);
        //                    slideTop >= b && (slideTop = b);
        //
        //                    _this[ele].left = slideLeft;
        //                    _this[ele].top = slideTop;
        //                };
        //                document.onmouseup = function () {
        //                    document.onmousemove = null;
        //                    document.onmouseup = null;
        //                };
        //                return false;
        //            }
        //        }
        //    }
        //});
        //
        //// 选择图片：分销中心首页
        //$('.j-image').selectImages({
        //    multiple: false,
        //    done: function (data) {
        //        appVue.$data.backdrop.src =  data[0].file_path;
        //        appVue.$data.qrcode.src    = data[2].file_path;
        //    }
        //});

        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            buildData: function () {
                return {
                    qrcode: appVue.$data
                };
            }
        });
        //提交数据
        $(".j-submit").click(function(){
            var $url = $(this).data('action'),
                val = $(this).data('id');
                 $btn=$(this);
            $.post($url,  {x: val}, function (data) {
                if (data.code ==1) {
                   $('file-down').data('file').valueOf()
                }else {
                    layer.alert(data.msg, {icon: 5});
                }

            }, "json");
        });
        //下载图片
        $("#file-down").click(function(){
            var  url =$(this).data('file');
            var  name =$(this).data('name');
           // var  name ='1599558126632.png';
            var x=new XMLHttpRequest();
            x.open("GET", url, true);
            x.responseType = 'blob';
            x.onload=function(e){
               // download(x.response, name+'.png', "image/png" );
                download(x.response,name, "image/png" );
            };
            x.send();
        });


        function download(data, strFileName, strMimeType) {
            var self = window, // this script is only for browsers anyway...
                u = "application/octet-stream", // this default mime also triggers iframe downloads
                m = strMimeType || u,
                x = data,
                D = document,
                a = D.createElement("a"),
                z = function(a){return String(a);},


                B = self.Blob || self.MozBlob || self.WebKitBlob || z,
                BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
                fn = strFileName || "download",
                blob,
                b,
                ua,
                fr;

            //if(typeof B.bind === 'function' ){ B=B.bind(self); }

            if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
                x=[x, m];
                m=x[0];
                x=x[1];
            }



            //go ahead and download dataURLs right away
            if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){
                return navigator.msSaveBlob ?  // IE10 can't do a[download], only Blobs:
                    navigator.msSaveBlob(d2b(x), fn) :
                    saver(x) ; // everyone else can save dataURLs un-processed
            }//end if dataURL passed?

            try{

                blob = x instanceof B ?
                    x :
                    new B([x], {type: m}) ;
            }catch(y){
                if(BB){
                    b = new BB();
                    b.append([x]);
                    blob = b.getBlob(m); // the blob
                }

            }



            function d2b(u) {
                var p= u.split(/[:;,]/),
                    t= p[1],
                    dec= p[2] == "base64" ? atob : decodeURIComponent,
                    bin= dec(p.pop()),
                    mx= bin.length,
                    i= 0,
                    uia= new Uint8Array(mx);

                for(i;i<mx;++i) uia[i]= bin.charCodeAt(i);

                return new B([uia], {type: t});
            }

            function saver(url, winMode){


                if ('download' in a) { //html5 A[download]
                    a.href = url;
                    a.setAttribute("download", fn);
                    a.innerHTML = "downloading...";
                    D.body.appendChild(a);
                    setTimeout(function() {
                        a.click();
                        D.body.removeChild(a);
                        if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(a.href);}, 250 );}
                    }, 66);
                    return true;
                }

                //do iframe dataURL download (old ch+FF):
                var f = D.createElement("iframe");
                D.body.appendChild(f);
                if(!winMode){ // force a mime that will download:
                    url="data:"+url.replace(/^data:([\w\/\-\+]+)/, u);
                }


                f.src = url;
                setTimeout(function(){ D.body.removeChild(f); }, 333);

            }//end saver


            if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
                return navigator.msSaveBlob(blob, fn);
            }

            if(self.URL){ // simple fast and modern way using Blob and URL:
                saver(self.URL.createObjectURL(blob), true);
            }else{
                // handle non-Blob()+non-URL browsers:
                if(typeof blob === "string" || blob.constructor===z ){
                    try{
                        return saver( "data:" +  m   + ";base64,"  +  self.btoa(blob)  );
                    }catch(y){
                        return saver( "data:" +  m   + "," + encodeURIComponent(blob)  );
                    }
                }

                // Blob but not URL:
                fr=new FileReader();
                fr.onload=function(e){
                    saver(this.result);
                };
                fr.readAsDataURL(blob);
            }
            return true;
        } /* end download() */


    });

</script>
